package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.CoachInterpretationModel;
import com.banshuibao.bsb_android.myinfterface.RecyclerItemListener;
import com.banshuibao.bsb_android.util.TimeUtil;
import com.banshuibao.bsb_android.util.WebViewCssUtil;
import com.banshuibao.bsb_android.util.WebViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyh on 2017/7/7.
 */

public class CoachInterpretationAdapter extends RecyclerView.Adapter<CoachInterpretationAdapter.ViewHolder> {

    private final static int VIEW_TYPE_TITLE = 0;
    private final static int VIEW_TYPE_OFFICIAL = 1;
    private final static int VIEW_TYPE_EXPERT = 2;
    private final static int VIEW_TYPE_POLICY = 3;
    private final static int VIEW_TYPE_EXPERT_LIST = 4;


    private static final int TITLE_1 = R.id.title_1;
    private static final int TITLE_2 = R.id.title_2;
    private static final int TITLE_3 = R.id.title_3;

    private Context mContext;

    private List<DataHolder> dataHolders;

    private LayoutInflater layoutInflater;

    private int type = R.id.title_2;

    private String official;

    private String expert;

    private List<CoachInterpretationModel.Relateds> relateds;

    private List<CoachInterpretationModel.PolicyQas> policyQases;

    private RecyclerItemListener<CoachInterpretationModel.Relateds> rL;

    private RecyclerItemListener<CoachInterpretationModel.PolicyQas> pL;

    public CoachInterpretationAdapter(Context context) {
        mContext = context;
        dataHolders = new ArrayList<>();
        layoutInflater = LayoutInflater.from(mContext);
        relateds = new ArrayList<>();
        policyQases = new ArrayList<>();
    }

    public void setrL(RecyclerItemListener<CoachInterpretationModel.Relateds> rL) {
        this.rL = rL;
    }

    public void setpL(RecyclerItemListener<CoachInterpretationModel.PolicyQas> pL) {
        this.pL = pL;
    }

    public void setOfficial(String official) {
        this.official = official;
    }

    public void setExpert(String expert) {
        this.expert = expert;
    }

    public void setRelateds(List<CoachInterpretationModel.Relateds> relateds) {
        if (this.relateds != null) {
            this.relateds.clear();
            this.relateds.addAll(relateds);
        }
    }

    public void setPolicyQases(List<CoachInterpretationModel.PolicyQas> policyQases) {
        if (this.policyQases != null) {
            this.policyQases.clear();
            this.policyQases.addAll(policyQases);
        }
    }

    public void initAdapter() {
        if (dataHolders != null) {
            dataHolders.clear();
            //TitleDataHolder titleDataHolder = new TitleDataHolder();
            //dataHolders.add(titleDataHolder);
            switch (type) {
                case R.id.title_1:
                    initOfficialDataHolder();
                    break;
                case R.id.title_2:
                    initExpertDataHolder();
                    break;
                case R.id.title_3:
                    initPolicyDataHolder();
                    break;
            }
            notifyDataSetChanged();
        }
    }

    public void setTitle(int type) {
        this.type = type;
        initAdapter();
    }

    private void initOfficialDataHolder() {
        OfficialDataHolder dataHolder = new OfficialDataHolder();
        dataHolder.setObject(official);
        dataHolders.add(dataHolder);
    }

    private void initExpertDataHolder() {
        ExpertDataHolder dataHolder = new ExpertDataHolder();
        dataHolder.setObject(expert);
        dataHolders.add(dataHolder);
        if (relateds != null && relateds.size() > 0) {
            int size = relateds.size();
            for (int i = 0; i < size; ++i) {
                ExpertListDataHolder listHolder = new ExpertListDataHolder();
                listHolder.setObject(relateds.get(i));
                dataHolders.add(listHolder);
            }
        }
    }

    private void initPolicyDataHolder() {
        if (policyQases != null && policyQases.size() > 0) {
            int size = policyQases.size();
            for (int i = 0; i < size; ++i) {
                PolicyDataHolder policyDataHolder = new PolicyDataHolder();
                policyDataHolder.setObject(policyQases.get(i));
                dataHolders.add(policyDataHolder);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return dataHolders != null && dataHolders.size() > 0 ? dataHolders.get(position).getType() : 0;
    }

    @Override
    public CoachInterpretationAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_TITLE:
                return new TitleViewHolder(layoutInflater.inflate(R.layout.item_coachinterpretation_title, parent, false));
            case VIEW_TYPE_OFFICIAL:
                return new OfficialViewHolder(layoutInflater.inflate(R.layout.item_coach_interpretation_webview, parent, false));
            case VIEW_TYPE_EXPERT:
                return new ExpertViewHolder(layoutInflater.inflate(R.layout.item_coach_interpretation_webview, parent, false));
            case VIEW_TYPE_EXPERT_LIST:
                return new ExpertListViewHolder(layoutInflater.inflate(R.layout.item_coach_interpretation_expert_list, parent, false));
            case VIEW_TYPE_POLICY:
                return new PolicyViewHolder(layoutInflater.inflate(R.layout.item_coach_interpretation_policy, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (dataHolders != null && dataHolders.size() > 0) {
            holder.setData(dataHolders.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return dataHolders != null ? dataHolders.size() : 0;
    }

    public abstract class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        public abstract int getType();

        public abstract void setData(DataHolder dataHolder);

    }

    private class TitleViewHolder extends ViewHolder {

        private LinearLayout l1, l2, l3;
        private TextView title1, title2, title3;
        private View line_1, line_2, line_3;
        private View.OnClickListener l = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == type)
                    return;
                type = v.getId();
                if (type == TITLE_1) {
                    title1.setTextColor(mContext.getResources().getColor(R.color.line_blue));
                    title2.setTextColor(mContext.getResources().getColor(R.color.text_black));
                    title3.setTextColor(mContext.getResources().getColor(R.color.text_black));
                    line_1.setBackgroundResource(R.color.line_blue);
                    line_2.setBackgroundResource(R.color.line_color);
                    line_3.setBackgroundResource(R.color.line_color);
                } else if (type == TITLE_2) {
                    title1.setTextColor(mContext.getResources().getColor(R.color.text_black));
                    title2.setTextColor(mContext.getResources().getColor(R.color.line_blue));
                    title3.setTextColor(mContext.getResources().getColor(R.color.text_black));
                    line_1.setBackgroundResource(R.color.line_color);
                    line_2.setBackgroundResource(R.color.line_blue);
                    line_3.setBackgroundResource(R.color.line_color);
                } else {
                    title1.setTextColor(mContext.getResources().getColor(R.color.text_black));
                    title2.setTextColor(mContext.getResources().getColor(R.color.text_black));
                    title3.setTextColor(mContext.getResources().getColor(R.color.line_blue));
                    line_1.setBackgroundResource(R.color.line_color);
                    line_2.setBackgroundResource(R.color.line_color);
                    line_3.setBackgroundResource(R.color.line_blue);
                }
                initAdapter();
            }
        };

        TitleViewHolder(View itemView) {
            super(itemView);
            title1 = itemView.findViewById(R.id.title_text1);
            title2 = itemView.findViewById(R.id.title_text2);
            title3 = itemView.findViewById(R.id.title_text3);
            l1 = itemView.findViewById(R.id.title_1);
            l2 = itemView.findViewById(R.id.title_2);
            l3 = itemView.findViewById(R.id.title_3);
            line_1 = itemView.findViewById(R.id.line_1);
            line_2 = itemView.findViewById(R.id.line_2);
            line_3 = itemView.findViewById(R.id.line_3);
            l1.setOnClickListener(l);
            l2.setOnClickListener(l);
            l3.setOnClickListener(l);
        }

        @Override
        public int getType() {
            return VIEW_TYPE_TITLE;
        }

        @Override
        public void setData(DataHolder dataHolder) {

        }
    }

    private class OfficialViewHolder extends ViewHolder {

        private WebView webView;

        public OfficialViewHolder(View itemView) {
            super(itemView);
            webView = (WebView) itemView.findViewById(R.id.webView);
            WebViewUtil.addImageClickListener(mContext, webView);
        }

        @Override
        public int getType() {
            return VIEW_TYPE_OFFICIAL;
        }

        @Override
        public void setData(DataHolder dataHolder) {
            String newHtml = WebViewCssUtil.setCss(official);
            webView.loadDataWithBaseURL(null, newHtml, "text/html", "utf-8", null);
        }
    }

    private class ExpertViewHolder extends ViewHolder {

        private WebView webView;

        private TextView title;

        public ExpertViewHolder(View itemView) {
            super(itemView);
            webView = (WebView) itemView.findViewById(R.id.webView);
            WebViewUtil.addImageClickListener(mContext, webView);
            title = (TextView) itemView.findViewById(R.id.list_title);
            title.setText("相关解读");
            title.setVisibility(View.VISIBLE);
        }

        @Override
        public int getType() {
            return VIEW_TYPE_EXPERT;
        }

        @Override
        public void setData(DataHolder dataHolder) {
            String newHtml = WebViewCssUtil.setCss(expert);
            webView.loadDataWithBaseURL(null, newHtml, "text/html", "utf-8", null);
        }
    }

    private class ExpertListViewHolder extends ViewHolder {

        private TextView title;
        private View line;

        public ExpertListViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            line = itemView.findViewById(R.id.line);
        }

        @Override
        public int getType() {
            return VIEW_TYPE_EXPERT_LIST;
        }

        @Override
        public void setData(DataHolder dataHolder) {
            ExpertListDataHolder dataHolder1 = (ExpertListDataHolder) dataHolder;
            final CoachInterpretationModel.Relateds item = dataHolder1.getObject();
            if (item != null) {
                title.setText(item.getTitle());
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (rL != null)
                            rL.onItemClickListener(itemView, item);
                    }
                });
            }
        }
    }

    private class PolicyViewHolder extends ViewHolder {

        private TextView title, content, time;

        public PolicyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            title.getPaint().setFakeBoldText(true);
            content = (TextView) itemView.findViewById(R.id.content);
            time = (TextView) itemView.findViewById(R.id.time);
        }

        @Override
        public int getType() {
            return VIEW_TYPE_POLICY;
        }

        @Override
        public void setData(DataHolder dataHolder) {
            PolicyDataHolder dataHolder1 = (PolicyDataHolder) dataHolder;
            final CoachInterpretationModel.PolicyQas item = dataHolder1.getObject();
            if (item != null) {
                title.setText(item.getTitle());
                content.setText(item.getContent());
                time.setText(TimeUtil.format(item.getCreateDate()));
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (pL != null)
                            pL.onItemClickListener(itemView, item);
                    }
                });
            }
        }
    }

    private abstract class DataHolder<T> {

        private T object;

        abstract int getType();

        public T getObject() {
            return object;
        }

        public void setObject(T object) {
            this.object = object;
        }
    }

    private class TitleDataHolder extends DataHolder<String> {

        @Override
        int getType() {
            return VIEW_TYPE_TITLE;
        }
    }

    private class OfficialDataHolder extends DataHolder<String> {

        @Override
        int getType() {
            return VIEW_TYPE_OFFICIAL;
        }
    }

    private class ExpertDataHolder extends DataHolder<String> {

        @Override
        int getType() {
            return VIEW_TYPE_EXPERT;
        }
    }

    private class ExpertListDataHolder extends DataHolder<CoachInterpretationModel.Relateds> {

        @Override
        int getType() {
            return VIEW_TYPE_EXPERT_LIST;
        }
    }

    private class PolicyDataHolder extends DataHolder<CoachInterpretationModel.PolicyQas> {

        @Override
        int getType() {
            return VIEW_TYPE_POLICY;
        }
    }

    public void clear() {
        if (relateds != null) {
            relateds.clear();
            relateds = null;
        }
        if (policyQases != null) {
            policyQases.clear();
            policyQases = null;
        }
        if (dataHolders != null) {
            dataHolders.clear();
            dataHolders = null;
        }
    }
}
